﻿using System;
using System.IO.Ports;
using Interface;

namespace Framework
{
    public class SerialCommunication : ICommunication
    {
        private SerialPort _serialPort;

        public string[] PortNames
        {
            get
            {
                string[] portNames = SerialPort.GetPortNames();
                Array.Sort(portNames, new AlphanumComparatorFast());
                return portNames;
            }
        }

        public string PortName
        {
            get
            {
                return _serialPort.PortName;
            }
            set
            {
                _serialPort.PortName = value;
            }
        }

        public SerialCommunication(string portName, int baudrate, Parity parity, int dataBits, StopBits stopBits)
        {
            _serialPort = new SerialPort(portName, baudrate, parity, dataBits, stopBits);
        }

        public int ReadTimeout
        {
            get { return _serialPort.ReadTimeout; }
            set { _serialPort.ReadTimeout = value; }
        }

        public int BytesToRead
        {
            get { return _serialPort.BytesToRead; }
        }

        public int ReadByte()
        {
            return _serialPort.ReadByte();
        }

        public void Write(byte[] buffer, int offset, int count)
        {
            _serialPort.Write(buffer, offset, count);
        }

        public void Write(string text)
        {
            _serialPort.Write(text);
        }

        public int Read(byte[] buffer, int offset, int count)
        {
            return _serialPort.Read(buffer, offset, count);
        }

        public void Open()
        {
            _serialPort.Open();
        }

        public void Close()
        {
            _serialPort.Close();
        }

        public void Dispose()
        {
            _serialPort.Dispose();
        }
    }
}
